﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Configuration;
using System.IO;
using PushSharp;

namespace Zatify.WebUI
{
    public class Utils
    {
        public class MyRequireHttpsAttribute : RequireHttpsAttribute
        {
            public override void OnAuthorization(AuthorizationContext filterContext)
            {
                if (filterContext == null)
                {
                    throw new ArgumentNullException("filterContext");
                }

                bool useHttps = false;
                if (WebConfigurationManager.AppSettings["UseHttps"] != null)
                    useHttps = Convert.ToBoolean(WebConfigurationManager.AppSettings["UseHttps"]);

                //if (filterContext.HttpContext != null && filterContext.HttpContext.Request.IsLocal)
                if (!useHttps)
                {
                    return;
                }

                //(filterContext.ActionDescriptor.ActionName == "")

                base.OnAuthorization(filterContext);
            }
        }

        public static bool CheckImageMime(string mime)
        {
            string[] allow = { "image/png", "image/gif", "image/bmp", "image/jpeg", "image/jpg" };
            return allow.Contains(mime);
        }
        
        public static void SendNotification(string deviceToken, string message, string alertMode)
        {
            using (var br = new PushBroker())
            {
                br.OnNotificationFailed += new PushSharp.Core.NotificationFailedDelegate(br_OnNotificationFailed);
                br.OnServiceException += new PushSharp.Core.ServiceExceptionDelegate(br_OnServiceException);
                br.OnChannelException += new PushSharp.Core.ChannelExceptionDelegate(br_OnChannelException);

                var appleCert = File.ReadAllBytes(Path.Combine(WebConfigurationManager.AppSettings["ApplePushServiceFolder"], "cert.p12"));
                br.RegisterAppleService(new PushSharp.Apple.ApplePushChannelSettings(appleCert, WebConfigurationManager.AppSettings["ApplePushServicePassword"]));

                var apNot = new PushSharp.Apple.AppleNotification()
                               .ForDeviceToken(deviceToken)
                               .WithAlert(message)
                               .WithBadge(1);
                if (alertMode == "AlertByTone")
                {
                    apNot.WithSound("sound.caf");
                }
                else
                {
                    if (alertMode == "AlertByVibration")
                    {
                        apNot.WithSound("vibration.caf");
                    }
                    else
                    {
                        apNot.WithSound("silence.caf");
                    }
                }

                br.QueueNotification(apNot);
                br.StopAllServices();
            }
        }

        public static void br_OnChannelException(object sender, PushSharp.Core.IPushChannel pushChannel, Exception error)
        {
            br_OnNotificationFailed(sender, null, error);
        }

        public static void br_OnServiceException(object sender, Exception error)
        {
            br_OnNotificationFailed(sender, null, error);
        }

        static void br_OnNotificationFailed(object sender, PushSharp.Core.INotification notification, Exception error)
        {
            File.AppendAllText(Path.Combine(WebConfigurationManager.AppSettings["ApplePushServiceFolder"], "errors.txt"), "\n" + DateTime.Now.ToString() + "\n" + error.Message + "\n" + error.StackTrace);
        }
    }
}